#!/bin/sh

## chang here
SERVICE_DIR=/Volumes/work/workspace/redp/target
SERVICE_NAME=redp-1.0.0
CONFIG_DIR=/Volumes/work/workspace/redp/target/classes
JAVA_HOME_DIR=/usr/bin

case "$1" in
        start)
                procedure=`ps -ef | grep -w "${SERVICE_NAME}" |grep -w "java"| grep -v "grep" `
                if [ "${procedure}" = "" ];
                then
                        echo "start ..."       
                        exec nohup ${JAVA_HOME_DIR}/java -Xms128m -Xmx512m -jar ${SERVICE_DIR}/${SERVICE_NAME}\.jar --spring.config.location=${CONFIG_DIR}/application.yml >/dev/null 2>&1 &
                        echo "start successful"
                else
                        echo "${SERVICE_NAME} is already started."
                fi
                ;;

        stop)
                procedure=`ps -ef | grep -w "${SERVICE_NAME}" |grep -w "java"| grep -v "grep" `
                if [ "${procedure}" = "" ];
                then
                        echo "${SERVICE_NAME} is terminated."
                else
                        kill -9 ${procedure}
                        sleep 1
                        argprocedure=`ps -ef | grep -w "${SERVICE_NAME}" |grep -w "java"| grep -v "grep" `
                        if [ "${argprocedure}" = "" ];
                        then
                                echo "${SERVICE_NAME} stop successful."
                        else
                                kill -9 ${argprocedure}
                                echo "${SERVICE_NAME} stop failed."
                        fi
                fi
                ;;

        restart)
                $0 stop
                sleep 1
                $0 start $2
                ;;

        *)
                echo "usage: $0 [start|stop|restart]"
                ;;
esac

